﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Net;
using System.Text;
using System.IO;
using System.Xml;

namespace StoreWeb.ajax
{
    public partial class message : System.Web.UI.Page
    {
        BLL.Message messagebll = new BLL.Message();
        BLL.Customer customer = new BLL.Customer();
        Maticsoft.BLL.UserAdmin user = new Maticsoft.BLL.UserAdmin();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int row = 0;
                if (!string.IsNullOrEmpty(Request["action"]) && Request["action"] == "delete")
                {
                    Response.Write((messagebll.Delete(Convert.ToInt32(Request["id"])) ? "1" : "0"));
                }
                else
                {
                    Model.Message ne = new Model.Message();
                    if (!string.IsNullOrEmpty(Request["id"]))
                    {
                        row = 3;
                    }
                    else
                    {
                        //Model.Customer u = customer.GetModel(Convert.ToInt32(Request["ddluser"]));
                        //ne.mobile = u.mobile;
                        //ne.username = u.customername;
                        //ne.userid = u.id;
                        //ne.smscontent = Request["smscontent"];
                        //ne.createtime = DateTime.Now;
                        //ne.createuser = user.GetModel(Convert.ToInt32(Request.Cookies["UserLogin"].Values[0])).username;
                        //ne.smstate = "发送中";
                        //row = messagebll.Add(ne) > 0 ? 1 : 0;

                        string str = Request["ddluser"].ToString();
                        string[] array = str.Split(',');
                        foreach (string i in array)
                        {
                            Model.Customer u = customer.GetModel(Convert.ToInt32(i));
                            ne.mobile = u.mobile;
                            ne.username = u.customername;
                            ne.userid = u.id;
                            ne.smscontent = Request["smscontent"];
                            ne.createtime = DateTime.Now;
                            ne.createuser = user.GetModel(Convert.ToInt32(Request.Cookies["UserLogin"].Values[0])).username;
                            ne.smstate = "发送中";
                            row = messagebll.Add(ne) > 0 ? 1 : 0;
                        }

                    }
                    if (row == 1)
                    {
                        Response.Write("1");
                    }else if (row == 2)
                    {
                        Response.Write("2");
                    }
                    else if (row == 3)
                    {
                        Response.Write("3");
                    }
                    else
                    {
                        Response.Write("0");
                    }
                }
            }
        }
    }
}